<?php

namespace app\admin\controller;
class WxFile extends \app\ApiRest
{
    protected $uid;

    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->uid = $this->getUserId();
    }

    public function uploadFile()
    {
        $input = $this->_param;
//        pr($input);
        $file = $this->request->file("file");
        if (empty($file)) {
            $file = $this->request->file("files");
        }
        if (empty($file)) {
            $file = $this->request->file("filePath");
        }
        if (empty($file)) {
            return $this->error("not file ,please check file.");
        }
        $uploda_model = new \app\Common\Upload($this->_uniacid);
        $type = "picture";
        if (isset($input["type"])) {
            $type = $input["type"];
        }
        $check = !empty($input["name"]) ? $input["name"] : "";
        $info = $uploda_model->upload($type, $file, [], $check);
//        var_dump($info);
        $result = false;
        if (!empty($info)) {
            if (isset($info["attachment"]) && !empty($info["attachment"])) {
                $info["attachment_path"] = $info["attachment"];
                $info = transImagesOne($info, ["attachment_path"], $this->_uniacid);
            }
            $info["uid"] = $this->uid;
            $info["from"] = "wx";
            $attachment_model = new \app\admin\model\CoreAttachment();
            $data = $attachment_model->createAttach($info);
            if (!empty($data)) {
                $result = $info;
            }
        }
        return $this->success($result);
    }

    public function uploadFiles()
    {
        $input = $this->_param;
        $files = $this->request->file("file");
        if (empty($files)) {
            $files = $this->request->file("files");
        }
        if (empty($files)) {
            $files = $this->request->file("filePath");
        }
        if (empty($files)) {
            return $this->error("not file ,please check file.");
        }
        $type = "picture";
        if (isset($input["type"])) {
            $type = $input["type"];
        }
        $result = [];
        $uploda_model = new \app\Common\Upload($this->_uniacid);
        foreach ($files as $file) {
            $info = $uploda_model->upload($type, $file);
            if (!empty($info)) {
                if (isset($info["attachment"]) && !empty($info["attachment"])) {
                    $info["attachment_path"] = $info["attachment"];
                    $info = transImagesOne($info, ["attachment_path"], $this->_uniacid);
                }
                $info["uid"] = $this->uid;
                $info["from"] = "wx";
                $attachment_model = new \app\admin\model\CoreAttachment();
                $data = $attachment_model->createAttach($info);
                if (!empty($data)) {
                    $result[] = $info;
                }
            }
        }
        return $this->success($result);
    }
}
